package com.cac.demo.service.msDeliveryCrew;

import com.cac.demo.dao.msDeliveryCrew.MsDeliveryCrewDao;
import com.cac.demo.util.adapter.DaoException;
import com.cac.demo.util.adapter.QueryReqBean;
import com.cac.demo.util.adapter.QueryRespBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MsDeliveryCrewService {
    @Autowired
    private MsDeliveryCrewDao msDeliveryCrewDao;
    /*配送人员查询*/
    public QueryRespBean<HashMap> msDeliveryCrewSearch(QueryReqBean<HashMap> queryReqBean, String orderBy, String Keywords){
        QueryRespBean<HashMap> queryRespBean = new QueryRespBean<>();
        try{
            PageHelper.startPage(queryReqBean.getPageParameter());
            HashMap searchParams = queryReqBean.getSearchParams();
            Page<HashMap> dataList = msDeliveryCrewDao.msDeliveryCrewSearch(searchParams,orderBy,Keywords);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        }catch (Exception e){
            throw  new DaoException(e.getMessage(),e);
        }
    }
    /*判断查询是否为空*/
    public List<HashMap> msDeliveryCrewSearch(HashMap searchParams, String orderBy, String Keywords){
        try{
            Page<HashMap> dataList = msDeliveryCrewDao.msDeliveryCrewSearch(searchParams,orderBy,Keywords);
            return dataList;
        }catch (Exception e){
            throw  new DaoException(e.getMessage(),e);
        }
    }
    /*配送人员新增*/
    @Transactional
    public int msDeliveryCrewInsert(Map map) {
        try{
            int count = msDeliveryCrewDao.msDeliveryCrewInsert(map);
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }
    /*配送人员修改*/
    @Transactional
    public int msDeliveryCrewUpdate(Map map){
        try{
            int count = msDeliveryCrewDao.msDeliveryCrewUpdate(map);
            if(count==0){
                throw new DaoException("修改数据失败");
            }
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }
    /*配送人员删除*/
    @Transactional
    public int msDeliveryCrewDelete(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new DaoException("删除失败！传入的参数主键为null");
        }
        try {
            //记录日志
            HashMap hashmap = msDeliveryCrewDao.msDeliveryCrewFindId(id);
            if (hashmap == null) {
                throw new DaoException("删除失败！对象不存在");
            }
//删除业务数据
            int count = msDeliveryCrewDao.msDeliveryCrewDelete(id);
            return count;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
}
